from __future__ import unicode_literals

from mongoengine import *

import pymongo
client = pymongo.MongoClient("mongodb://localhost:27017")
connect('anstack')

m_db = client.anstack


def update_para_value(host_id, para_name, para_value):
    result = m_db.host.update_one(
        {"_id": host_id},
        {"$set": {para_name: para_value}}
    )
    return result


def delete_host_service(host_id, service):
    result = m_db.host.update_one(
        {"_id": host_id},
        {"$unset": {service: 0}}
    )
    return result


class Host(Document):
    _id = StringField(verbose_name="host name", min_length=3, max_length=50, required=True, unique=True, primary_key=True)
    customer_id = StringField(required=True)
    ip = StringField(max_length=255, required=True)
    port = StringField(default=22, required=False)
    server_lvm = BooleanField(default=False)
    service = DictField(required=False)

    # Variables and attributes may not begin with underscores, so use this get host_name
    def host_name(self):
        return self._id

    def service_name(self):
        return self.service.keys()


class Service(Document):
    _id = StringField(verbose_name="service name", max_length=30, required=True, unique=True)
    parameters = DictField(required=True)
